<?php
namespace app\api\controller;

use app\api\model\Order as OrderModel;
use app\api\model\Express as ExpressModel;
use app\api\model\OrderDelivery;
use app\api\model\Setting;
use think\Db;

/**
 * 订单管理
 * Class Order
 * @package app\user\controller
 */
class Order extends Controller
{
    public function initialize()
    {
        $this->zonguser = $this->getUser();   // 商户信息
        $this->user = $this->getShangUser();   // 商户信息
        $this->imgadess = $this->pcUrl();
    }
    protected  $pay_type = ['未付款','微信付款','支付宝','余额','线下'];

    protected $orderTypes = ['10'=>'未付款', '11'=>'未付款取消', '12'=>'付款取消', '13'=>'未发货', '14'=>'已发货','15'=>'已收货'];


    /**
     * 待处理售后列表
     */
    public function etcSaleList($dataType='etcsalelist',$user_id='')
    {
        $model = new OrderModel;
        $list = $model->getLists('etcsalelist', $this->request->get(),$user_id);
        return $this->renderSuccess(1000,'成功',compact('dataType', 'list'));
    }

    /**
     * 已处理售后列表
     */
    public function inSaleList($dataType='inSaleList',$user_id='')
    {
        $model = new OrderModel;
        $list = $model->getLists($dataType, $this->request->get(),$user_id);
        return $this->fetch('insale', compact('title', 'dataType', 'list'));
    }
    /**
     * 售后详情
     */
    public function saleDetail($order_id)
    {
        return $this->fetch('saledetail', [
            'detail' => OrderModel::detail($order_id),
            'express_list' => ExpressModel::getAll()
        ]);
    }
    /**
     * 确认发货
     */
    public function delivery($order_id)
    {

        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('order'))) {
            return $this->renderSuccess(1000,'发货成功');
        }
        return $this->renderError(1001,$model->getError() ?: '发货失败');
    }
    /**
     * 修改订单价格
     */
    public function updatePrice($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->updatePrice($this->postData('order'))) {
            return $this->renderSuccess(1000,'修改成功');
        }
        return $this->renderError(1001,$model->getError() ?: '修改失败');
    }
    /**
     * 订单列表
     */
    private function getList($title, $dataType,$user_id)
    {
        $model = new OrderModel;
        $list = $model->getLists($dataType, $this->request->post(),$user_id);
        return $this->renderSuccess( 1000,'成功',compact('title', 'dataType', 'list'));
    }








    /**
     * 订单列表
     */
    public function orderList()
    {
        $zhuangtai = $this->request->post();
        $user_id = $this->user['merchant_user_id'];
        $model = new OrderModel;

        $where['merchant_id']=$user_id;
        if(!empty($zhuangtai['type'])){
            $where['order_types']=$zhuangtai['type'];
        }
        $where['state']=$zhuangtai['tates'];

        $field = 'order_id,order_no,total_price,pay_price,order_types,create_time';
        $list = $model->getListsad($where,$field);
        if(count($list['data'])>0){
            $goodfield = 'order_goods_id,item_id,name,goods_attr,goods_price,total_num,image';
            foreach ($list['data'] as $key=>$val){
                $list['data'][$key]['create_time'] = date('Y-m-d H:i',$val['create_time']);
                if($zhuangtai['tates'] == '5'||$zhuangtai['tates'] == '6'){
                    $list['data'][$key]['jindu'] = $this->jinduXingqing($val['order_id']);
                }
                $goodwhere = [];
                $goodwhere['order_id'] = $val['order_id'];
                $goodlist = $model->orderGoods($goodwhere,$goodfield);
                $goodnum =0;
                if(count($goodlist)>0){
                    foreach ($goodlist as $a=>$b){
                        $goodnum = $goodnum+$b['total_num'];
                        if(!empty($b['image'])){
                            $goodlist[$a]['image'] = $this->imgadess.$b['image'].'.jpg';
                        }
                    }
                }
                $list['data'][$key]['goodlist'] = $goodlist;
                $list['data'][$key]['goodnum'] = $goodnum;
            }
        }
        return $this->renderSuccess( 1000,'成功',compact('list'));
    }

    /**
     * 订单详情
     */
    public function orderDetail()
    {
        $info = $this->request->post();
        if(empty($info['orderid'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        //$where['bfb_order.merchant_id'] =$this->user['merchant_id'];
        $where['bfb_order.order_id'] =$info['orderid'];
        $detail=OrderModel::orderdetail($where);
        $detail['create_time'] = date('Y-m-d H:i',$detail['create_time']);
        if($info['tates'] == '5'||$info['tates'] == '6'){
            $detail['jindu'] = $this->jinduXingqing($detail['order_id']);
        }
        $goods =OrderModel::goodslist($info['orderid']);
        if(count($goods)>0){
            foreach ($goods as $a=>$b){
                if(!empty($b['image'])){
                    $goods[$a]['image'] = $this->imgadess.$b['image'].'.jpg';
                }
            }
        }
        $detail['goods'] = $goods;
        $address=OrderModel::adress($info['orderid']);
        if(!empty($detail)){
            $detail['pay_typename']=$this->pay_type[$detail['pay_type']];
        }
        if(!empty($address)){
            $address['juti'] = $address['province_name'].$address['city_name'].$address['region_name'].$address['detail'];
        }

        return $this->renderSuccess( 1000,'成功',compact('detail', 'address'));

    }


    public function jinduXingqing($id)
    {
        $where['order_id'] = $id;
        $address=OrderModel::jindufind($where);
        if(count($address)>0){
            foreach ($address as $key=>$val){
                $ing = explode(',',$val['detail']);
                if(count($ing)>0){
                    foreach ($ing as $k=>$v){
                        $ing[$k] = $this->imgadess.$v.'.jpg';
                    }
                }
                $address[$key]['img'] = $ing;
            }
        }
        return $address;
    }

    /**
     * 修改订单价格
     */
    public function uporderPrice()
    {
        $info = $this->request->post();
        if(empty($info['id'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $where['bfb_order.merchant_id'] =$this->user['merchant_id'];
        $where['bfb_order.order_id'] =$info['id'];
        $model = OrderModel::orderpul($where);
        $mod = new OrderModel();
        $lsiy = OrderModel::updatice($this->request->post(),$model);
        if($lsiy) {
            return $this->renderSuccess(1000,'修改成功');
        }
        return $this->renderError($info,'修改失败',$date=[]);
    }


    /**
     * 取消订单
     */
    public function orderdel()
    {
        $info = $this->request->post();
        if(empty($info['id'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $where['bfb_order.merchant_id'] =$this->user['merchant_id'];
        $where['bfb_order.order_id'] =$info['id'];

        $detail=OrderModel::orderdetail($where);
        /*if(!empty($detail)&&$detail['pay_status'] == '20'){
            return $this->renderError(1002,'订单已付款,不能取消');
        }*/
        //$detail['goods']=OrderModel::goodslist($info['id']);
        if($detail['order_types'] == '10'){
            $order_types = '11';
        }elseif($detail['order_types'] == '13'){
            $order_types = '12';
        }elseif($detail['order_types'] == '14'||$detail['order_types'] == '15'){
            $order_types = '16';
        }else{
            return $this->renderError(1001,'订单类型错误');
        }
       $info = OrderModel::orderdel($info['id'],$this->user['merchant_id'],$order_types);

        if($info == '1000') {
            return $this->renderSuccess(1000,'成功');
        }
        return $this->renderError($info,'失败',$date=[]);
    }

    public function xiangxi()
    {
        $info = $this->request->post();
        if(empty($info['id'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $list['item'] = \app\common\model\Item::detailad($info['id']);
        $list['ping'] = OrderModel::pingjialist($info['id']);
        if(!empty($list['ping'])){
            foreach ($list['ping'] as $key =>$value){
                $list['ping'][$key]['zilist'] = OrderModel::pingjialistad($value['item_ping_id']);
            }
        }
        return $this->renderSuccess(1000,'成功',$list);
    }

    public function xiangxiadd()
    {
        $info = $this->request->post();
        if(empty($info['id'])||empty($info['txt'])){
            return $this->renderError(1001,'参数不能为空');
        }
        if(OrderModel::pingjialistadd($info)){
            return $this->renderSuccess(1000,'成功');
        }
        return $this->renderError(1002,'失败');
    }

    /*
     * 首页统计
     * */
    public function shangTongji()
    {
        $order = new OrderModel();
        $list['user']['onedaymen'] = $order->rderTotalPrice($this->user['merchant_id'],date('Y-m-d',time()));//某天的总
        $list['user']['daymen'] = $order->rderTotalmon($this->user['merchant_id']);//总营业额
        $list['user']['onedayding'] = $order->rderTotal($this->user['merchant_id']);//订单总
        $list['user']['dayding'] = $order->onerderTotal($this->user['merchant_id'],date('Y-m-d',time()));//一天订单
        return $this->renderSuccess(1000,'成功',$list);
    }



    /*
     * 商户首页
     * */
    public function shanghuindex()
    {
        $order = new OrderModel();
        $where['merchant_id']=$this->user['merchant_user_id'];
        $zhuangtai = $this->request->post();
        $where['state']=$zhuangtai['tates'];
        $field = 'order_id,order_no,total_price,pay_price,order_types,create_time';
        $ordelisy = $order->getListsTwo($where,$field);
        if(count($ordelisy)>0){
            $goodfield = 'order_goods_id,item_id,name,goods_attr,goods_price,total_num,image';
            foreach ($ordelisy as $key=>$val){
                $ordelisy[$key]['create_time'] = date('Y-m-d H:i',$val['create_time']);
                $goodwhere = [];
                $goodwhere['order_id'] = $val['order_id'];
                $goodlist = $order->orderGoods($goodwhere,$goodfield);
                if(count($goodlist)>0){
                    foreach ($goodlist as $a=>$b){
                        $goodlist[$a]['imagead'] = $this->imgadess.$b['image'].'.jpg';
                    }
                }
                $ordelisy[$key]['goodlist'] = $goodlist;
                $ordelisy[$key]['address'] = $order->orderAddress($goodwhere);
            }
        }
        return $this->renderSuccess(1000,'成功',$ordelisy);
    }

    /*
     * 全部评价
     * */
    public function xiangxiList()
    {
        $info =$this->request->post();
        $useid = $this->zonguser['user_id'];
        $itemlist = OrderModel::itemlist($useid,$info['tates']);
        $goodis = array_column($itemlist,'goods_id');
        $list = OrderModel::xiangxiList($goodis);
        if(count($list['data'])>0){
            foreach ($list['data'] as $key =>$value){
                $ziji = OrderModel::pingjialistad($value['item_ping_id']);
                if(!empty($info['type'])){
                    if($info['type'] ==  '1'){
                        if(!empty($ziji)){
                            unset($list['data'][$key]);
                        }
                    }else{
                        if(empty($ziji)){
                            unset($list['data'][$key]);
                        }else{
                            $list['data'][$key]['zilist'] =$ziji;
                        }
                    }
                }else{
                    $list['data'][$key]['zilist'] =$ziji;
                }

            }
        }
        $list['data'] = array_values($list['data']);
        return $this->renderSuccess(1000,'成功',$list);
    }

    /*
     *系统消息
     * */
    public function logList()
    {
        $user = $this->zonguser;
        $info =$this->request->post();
        $model = new OrderModel;
        $loglist = $model->logList($user['user_id'],$info['tates']);
        $list['sum'] = count($loglist);
        $loglistad = $model->logListad($user['user_id'],$info['tates']);
        $list['date'] = array_merge_recursive($loglist,$loglistad);

        return $this->renderSuccess(1000,'成功',$list);
    }

    /*
     *系统消息
     * */
    public function logfind()
    {
        $info =$this->request->post();
        if(empty($info['merchant_log_id'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $model = new OrderModel;
        $list = $model->logfind($info['merchant_log_id']);
        return $this->renderSuccess(1000,'成功',$list);
    }

    /*
     *系统消息已读
     * */
    public function logListUp()
    {
        $user = $this->zonguser;
        $info =$this->request->post();
        $where = [];
        if(!empty($info['merchant_log_id'])){
            $where['merchant_log_id'] =$info['merchant_log_id'];
        }

        $model = new OrderModel;
        if($model->logListUp($user['user_id'],$info['tates'],$where)){
            return $this->renderSuccess(1000,'成功');
        }else{
            return $this->renderSuccess(1000,'失败');
        }

    }

    /*
     *系统消息已读
     * */
    public function jinduAdd()
    {
        $info =$this->request->post();
        if(empty($info['name'])||empty($info['detail'])||empty($info['order_id'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $where['name'] =$info['name'];
        $where['detail'] =$info['detail'];
        $where['order_id'] =$info['order_id'];
        $where['create_time'] = time();
        $model = new OrderModel;
        if($model->jinduAdd($where)){
            return $this->renderSuccess(1000,'成功');
        }else{
            return $this->renderSuccess(1000,'失败');
        }
    }

    /*
     *系统消息已读
     * */
    public function querenFahuo()
    {
        $info =$this->request->post();
        if(empty($info['orderid'])){
            return $this->renderError(1001,'ID参数不能为空');
        }
        $model = new OrderModel;
        $where['order_id'] = $info['orderid'];
        $order = $model->orderpul($where);
        if(!empty($order)&&$order['order_types'] == '13'){
            $upda['order_types'] = '14';
            if($info['tates'] != '5'&&$info['tates'] != '6'){
                if(empty($info['express_no'])){
                    return $this->renderError(1001,'参数不能为空');
                }
                $upda['express_no'] = $info['express_no'];
            }
            $list = $model->querenFahuo($where,$upda);
            if($list){
                return $this->renderSuccess(1000,'成功');
            }
        }
        return $this->renderError(1000,'失败');
    }

}